﻿using Prism.Events;
using QH_MSS_WPF_Core.Models;
using System.Collections.ObjectModel;

namespace OP20.ViewModels
{
    public class AllStationViewModel : BindableBase
    {
        private readonly IRegionManager _regionManager;
        private readonly IEventAggregator _eventAggregator;

        public AllStationViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {

            _regionManager = regionManager;
            _eventAggregator = eventAggregator;
            _eventAggregator.GetEvent<EventWarning>().Subscribe(DoWarnings);

            //开启定时器任务
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(200);
            dispatcherTimer.Tick += OnTick;
            dispatcherTimer.Start();

        }
        private int _i = 0;

        private void OnTick(object? sender, EventArgs e)
        {
            //闪烁
            if (_i >= 5)
            {
                _i = 0;
            }

            if (_i <= 3)
            {
                //亮
                //OP30

                ST1 = ST1Station_Global.stationPLCToPC.nStationStatus;
                ST2 = ST2Station_Global.stationPLCToPC.nStationStatus;
                ST3 = ST3Station_Global.stationPLCToPC.nStationStatus;
                ST4 = ST4Station_Global.stationPLCToPC.nStationStatus;
                ST5 = ST5Station_Global.stationPLCToPC.nStationStatus;
                ST6 = ST6Station_Global.stationPLCToPC.nStationStatus;
                ST8 = ST8Station_Global.stationPLCToPC.nStationStatus;
                ST10 = ST10Station_Global.stationPLCToPC.nStationStatus;
                ST11 = ST11Station_Global.stationPLCToPC.nStationStatus;
                ST12 = ST12Station_Global.stationPLCToPC.nStationStatus;
                ST13 = ST13Station_Global.stationPLCToPC.nStationStatus;
                ST14 = ST14Station_Global.stationPLCToPC.nStationStatus;
                ST15 = ST15Station_Global.stationPLCToPC.nStationStatus;


            }
            else
            {
                //灭
                //OP20
                ST1 = 0;
                ST2 = 0;
                ST3 = 0;
                ST4 = 0;
                ST5 = 0;
                ST6 = 0;
                ST8 = 0;
                ST10 = 0;
                ST11 = 0;
                ST12 = 0;
                ST13 = 0;
                ST14 = 0;
                ST15 = 0;
            }
            _i++;



            //桁架加药状态显示

            ST04Add = OtherDevicePLCToPC_Global.NeedleOperation[0];
            ST08Add = OtherDevicePLCToPC_Global.NeedleOperation[1];
            ST4Temp = OtherDevicePLCToPC_Global.ODAndTemp[2];
            ST4Humi = OtherDevicePLCToPC_Global.ODAndTemp[3];
            ST8Temp = OtherDevicePLCToPC_Global.ODAndTemp[4];
            ST8Humi = OtherDevicePLCToPC_Global.ODAndTemp[5];
        }

        private void DoWarnings(ObservableCollection<WarningModel> collection)
        {
            WarningData = collection;
        }

        #region 导航事件
        /// <summary>
        /// 工站导航
        /// </summary>
        private DelegateCommand<string> _NavigateCommand;
        public DelegateCommand<string> NavigateCommand =>
            _NavigateCommand ?? (_NavigateCommand = new DelegateCommand<string>(ExecuteNavigateCommand));

        void ExecuteNavigateCommand(string parameter)
        {

            NavigationParameters param = new NavigationParameters();
            param.Add("StationName", parameter);
            param.Add("NavigateFrom", AppViews.AllStationView);


            //根据参数跳转到对应的工站页面
            switch (parameter)
            {

                case "ST01":
                    _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST01View, param);
                    break;
                case "ST02":
                    _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST02View, param);
                    break;
                case "ST03":
                    _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST03View, param);
                    break;
                case "ST04":
                    _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST04View, param);
                    break;
                case "ST05":
                    _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST05View, param);
                    break;
                case "ST06":
                    _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST06View, param);
                    break;
                case "ST08":
                    _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST08View, param);
                    break;
                case "ST10":
                    _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST10View, param);
                    break;
                case "ST11":
                    _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST11View, param);
                    break;
                case "ST12":
                    _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST12View, param);
                    break;
                case "ST13":
                    _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST13View, param);
                    break;
                case "ST14":
                    _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST14View, param);
                    break;
                case "ST15":
                    _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST15View, param);
                    break;
                default:
                    break;
            }


        }
        #endregion

        #region 属性

        private ObservableCollection<WarningModel> _WarningData;


        public ObservableCollection<WarningModel> WarningData
        {
            get { return _WarningData; }
            set { SetProperty(ref _WarningData, value); }
        }



        private int _ST1;

        public int ST1
        {
            get { return _ST1; }
            set { SetProperty(ref _ST1, value); }
        }


        private int _ST2;
        public int ST2
        {
            get { return _ST2; }
            set { SetProperty(ref _ST2, value); }
        }

        private int _ST3;

        public int ST3
        {
            get { return _ST3; }
            set { SetProperty(ref _ST3, value); }
        }

        private int _ST4;
        public int ST4
        {
            get { return _ST4; }
            set { SetProperty(ref _ST4, value); }
        }
        private int _ST5;

        public int ST5
        {
            get { return _ST5; }
            set { SetProperty(ref _ST5, value); }
        }



        private int _ST6;
        public int ST6
        {
            get { return _ST6; }
            set { SetProperty(ref _ST6, value); }
        }

        private int _ST8;
        public int ST8
        {
            get { return _ST8; }
            set { SetProperty(ref _ST8, value); }
        }

        private int _ST10;
        public int ST10
        {
            get { return _ST10; }
            set { SetProperty(ref _ST10, value); }
        }
        private int _ST11;

        public int ST11
        {
            get { return _ST11; }
            set { SetProperty(ref _ST11, value); }
        }


        private int _ST12;
        public int ST12
        {
            get { return _ST12; }
            set { SetProperty(ref _ST12, value); }
        }
        private int _ST13;

        public int ST13
        {
            get { return _ST13; }
            set { SetProperty(ref _ST13, value); }
        }

        private int _ST14;
        public int ST14
        {
            get { return _ST14; }
            set { SetProperty(ref _ST14, value); }
        }

        private int _ST15;
        public int ST15
        {
            get { return _ST15; }
            set { SetProperty(ref _ST15, value); }
        }


        private bool _ST04Add;
        public bool ST04Add
        {
            get { return _ST04Add; }
            set { SetProperty(ref _ST04Add, value); }
        }


        private bool _ST08Add;
        public bool ST08Add
        {
            get { return _ST08Add; }
            set { SetProperty(ref _ST08Add, value); }
        }


        private float _ST4Temp;
        public float ST4Temp
        {
            get { return _ST4Temp; }
            set { SetProperty(ref _ST4Temp, value); }
        }

        private float _ST8Temp;
        public float ST8Temp
        {
            get { return _ST8Temp; }
            set { SetProperty(ref _ST8Temp, value); }
        }

        private float _ST4Humi;
        public float ST4Humi
        {
            get { return _ST4Humi; }
            set { SetProperty(ref _ST4Humi, value); }
        }

        private float _ST8Humi;
        public float ST8Humi
        {
            get { return _ST8Humi; }
            set { SetProperty(ref _ST8Humi, value); }
        }
        #endregion

    }
}
